package zyy.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import zyy.model.BeanMerchant;
import zyy.util.BusinessException;
import zyy.util.DBUtil;
import zyy.util.DbException;

public class MerchantManager {
  public List<BeanMerchant> loadALL() throws DbException {
    List<BeanMerchant> result = new ArrayList<BeanMerchant>();
    Connection conn = null;
    try {
      conn = DBUtil.getConnection();
      String sql = "select MID,mName,mStars,mCostAve,mCostCnt from Merchant ";
      java.sql.PreparedStatement pst = conn.prepareStatement(sql);
      java.sql.ResultSet rs = pst.executeQuery();
      while (rs.next()) {
        BeanMerchant p = new BeanMerchant();
        p.setMID(rs.getInt(1));
        p.setmName(rs.getString(2));
        p.setmStars(rs.getDouble(3));
        p.setmCostAve(rs.getDouble(4));
        p.setmCostCnt(rs.getDouble(5));
        result.add(p);
      }
      rs.close();
      pst.close();
    } catch (SQLException e) {
      e.printStackTrace();
      throw new DbException(e);
    } finally {
      if (conn != null)
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
    }
    return result;
  }

  public void add(String mName, int AddressID) throws BusinessException {
    // 不担心重名 直接 Insert
    if (mName == null || mName.equals("") || mName.length() > 255) {
      throw new BusinessException("新建商家名不应该为空\n或长于255字符");
    }
    if (!(AddressID > 0)) {
      throw new BusinessException("请选择新建商家的地址");
    }
    Connection conn = null;
    try {
      conn = DBUtil.getConnection();
      String sql = "INSERT INTO Merchant(mName, mCostAve, mCostCnt, mStars, AddressID) Values(?, 0, 0, 0, ?)";
      java.sql.PreparedStatement pst = conn.prepareStatement(sql);
      pst.setString(1, mName);
      pst.setInt(2, AddressID);
      pst.executeUpdate();
      pst.close();
    } catch (SQLException e) {
      e.printStackTrace();
      throw new BusinessException(e.getMessage());
    } finally {
      if (conn != null)
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
    }
  }

  public void unadd(int MID) throws BusinessException {
    // 如果有商品分类，则不允许删除
    // 否则，单纯删除商家本身
    Connection conn = null;
    try {
      conn = DBUtil.getConnection();
      String sql = "SELECT pd.productDirID FROM Product_Dir pd WHERE pd.MID=?";
      java.sql.PreparedStatement pst = conn.prepareStatement(sql);
      pst.setInt(1, MID);
      java.sql.ResultSet rs = pst.executeQuery();
      if (rs.next()) {
        throw new BusinessException("不可删除：\n该商家已有商品分类");
      }
      sql = "DELETE FROM Merchant WHERE MID=?";
      pst = conn.prepareStatement(sql);
      pst.setInt(1, MID);
      pst.executeUpdate();
      rs.close();
      pst.close();
    } catch (SQLException e) {
      e.printStackTrace();
      throw new BusinessException(e.getMessage());
    } finally {
      if (conn != null)
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
    }

  }
}